<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <!-- 放置页签 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="role">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px;">
              <el-button
                type="primary"
                size="small"
                icon="el-icon-plus"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              border
              :data="list"
            >
              <el-table-column
                type="index"
                label="序号"
                width="120"
                align="center"
              />
              <el-table-column
                prop="name"
                label="角色名"
                width="240"
                align="center"
              />
              <el-table-column
                prop="description"
                label="描述"
                align="center"
              />
              <el-table-column
                prop="address"
                label="操作"
                align="center"
              >
                <!-- 通过阅读table文档可得 slot-scope可以接受一个数据对象 其中包括当前点击的row对象数据 -->
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">修改</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                layout="prev, pager, next"
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="companyInfo">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="企业名称">
                <el-input v-model="companyInfo.name" disabled style="width: 600px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width: 600px" />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="companyInfo.companyPhone" disabled style="width: 600px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width: 600px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width: 600px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 修改role信息弹层 -->
    <!-- 关闭弹层会触发close事件 -->
    <el-dialog
      :title="roleForm.id ? '编辑角色' : '新增角色'"
      :visible="showDialog"
      width="45%"
      @close="btnCancel"
    >
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center" align="middle">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹层 -->
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <el-tree
        ref="permTree"
        :data="permData"
        show-checkbox
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="defaultSelect"
        :props="defaultProps"
        :check-strictly="true"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="btnPermCancel">取消</el-button>
          <el-button type="primary" @click="btnPermOk">确认</el-button>
        </el-col>
      </el-row>

    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  delRole,
  getRoleDetail,
  updateRole,
  addRole,
  assignPerm
} from '@/api/settings'
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeDate } from '@/utils'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'role',
      list: [], // 存储角色数据列表
      page: {
        page: 1, // 请求数据的页码
        pagesize: 10, // 每页条数
        total: 0 // 数据总条数
      },
      companyInfo: {}, // 公司信息
      roleData: {}, // 角色详情
      showDialog: false, // 控制编辑role信息弹层显示
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          {
            required: true,
            trigger: 'blur',
            message: '角色名不能为空'
          }
        ]
      },
      showPermDialog: false,
      selectCheck: [], // 存储当前角色已选中权限
      defaultSelect: [], // 存储默认选中的权限
      permData: [], // 存储所有角色权限点
      defaultProps: {
        label: 'name'
      },
      roleId: null
    }
  },
  created() {
    this.loadRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 加载角色列表
    async loadRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
      // console.log(total, rows)
    },
    // 点击分页按钮 - 页数变化
    currentChange(currentPage) {
      this.page.page = currentPage
      this.loadRoleList(this.page)
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.getters.companyId)
    },
    // 删除角色
    async delRole(roleId) {
      try {
        // 用await 限制 - 只有当其点了确定才执行下面的代码
        // 但await只管点击确定 那当点击cancel时会报错这时用try-catch捕获点击cancel的情况
        await this.$confirm('是否删除该角色')
        // 发起删除角色请求
        await delRole(roleId)
        // 重新拉取数据
        this.loadRoleList()
        // 提示删除成功
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑角色
    async editRole(roleId) {
      // 先获取数据在弹层
      const result = await getRoleDetail(roleId)
      // console.log(result)
      this.roleForm = result
      this.showDialog = true
    },
    // 点击取消按钮
    btnCancel() {
      // 清空数据 还原为空对象
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清空校验
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 点击确定按钮
    async btnOk() {
      try {
        await this.$refs.roleForm.validate()
        // 只有验证通过了才执行下面 更新接口
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          // 新增数据
          await addRole(this.roleForm)
        }
        // 重新拉取数据
        this.loadRoleList()
        this.showDialog = false
        this.roleForm.id ? this.$message.success('修改成功') : this.$message.success('新增角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    addRole() {
      this.showDialog = true
    },
    // 分配权限
    async assignPerm(id) {
      this.permData = tranListToTreeDate(await getPermissionList(), '0')
      const { permIds } = await getRoleDetail(id)
      this.defaultSelect = permIds
      this.roleId = id
      this.showPermDialog = true
    },
    // 点击分配权限的 确定按钮
    btnPermCancel() {
      this.defaultSelect = [] // 重置数据
      this.showPermDialog = false
    },
    // 点击分配权限的 取消按钮
    async btnPermOk() {
      this.selectCheck = this.$refs.permTree.getCheckedKeys()
      await assignPerm({ permIds: this.selectCheck, id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    }
  }
}
</script>

<style scoped>

</style>
